﻿/***************************************************************************************
   Date    : February 7, 2007
   Purpose : Implement the BatsmanInfoCollection Class
   Maintenance Log : 
   Revision		Date			Description 
   --------		---------		-----------------
	1.0		    7/02/2007		Initial Release

****************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Security.Permissions;


namespace Hclt.Msft.Til.CricketScoreboard
{
    /// <summary>
    /// Holds The BatsmanInfo Class Object.
    /// </summary>
    [Serializable]
    public class BatsmanInfoCollection : CollectionBase, IList<BatsmanInfo>, ISerializable
    {
        #region Constructors

        /// <summary>
        /// 0 Argument Constructor Of The BatsmanInfoCollection Class.
        /// </summary>
        public BatsmanInfoCollection() { }


        /// <summary>
        /// 2 Argument Constructor Of The BatsmanInfoCollection Class.
        /// </summary>
        protected BatsmanInfoCollection(SerializationInfo info, StreamingContext context)
        {
            for (int i = 0; i < List.Count; i++)
            {
                this[i] = (BatsmanInfo)info.GetValue("Batsman", Type.GetType("BatsmanInfo"));
            }
        }

        #endregion
        
        #region Public Methods

        /// <summary>
        /// Adds New BatsmanInfo Object In List.
        /// </summary>
        public void Add(BatsmanInfo item)
        {
            List.Add(item);
        }

        /// <summary>
        /// Removes Already Existing BatsmanInfo Object From The List Using Its Index.
        /// </summary>
        public void Remove(int index)
        {
            if (index < 0 || index > Count - 1)
            {
                throw new IndexOutOfRangeException();
            }
            else
            {
                List.RemoveAt(index);
            }
        }

        /// <summary>
        /// Indexer To Returns BatsmanInfo Object From The List Using Index.
        /// </summary>
        public BatsmanInfo this[int index]
        {
            set
            {
                if (index < 0 || index > Count - 1)
                {
                    throw new IndexOutOfRangeException();
                }
                else
                {
                    List[index] = value;
                }
            }
            get
            {
                return (BatsmanInfo)List[index];
            }
        }

        /// <summary>
        /// Returns The Index Of The Particular BatsmanInfo Object In List.
        /// </summary>
        public int IndexOf(BatsmanInfo item)
        {
            return List.IndexOf(item);
        }

        /// <summary>
        /// Inserts A New BatsmanInfo Object In List At Any Particular Index.
        /// </summary>
        public void Insert(int index, BatsmanInfo item)
        {
            List.Insert(index, item);
        }

        /// <summary>
        /// Returns Whether A Particular BatsmanInfo Object Exists In The List.
        /// </summary>
        public bool Contains(BatsmanInfo item)
        {
            return List.Contains(item);
        }

        /// <summary>
        /// Removes A Particular BatsmanInfo Object From The List & Return true If It Succeded.
        /// </summary>
        public bool Remove(BatsmanInfo item)
        {
            List.Remove(item);
            return true;
        }

        /// <summary>
        /// Returns Whether The List Is Read Only Or Not.
        /// </summary>
        public bool IsReadOnly
        {
            get
            {
                return List.IsReadOnly;
            }
        }

        /// <summary>
        /// Copies The Whole List Into The Supplied BatsmanInfo Array.
        /// </summary>
        public void CopyTo(BatsmanInfo[] array, int arrayIndex)
        {
            if (Object.ReferenceEquals(array, null))
            {
                throw new ArgumentNullException();
            }

            if (arrayIndex < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            if (array.Rank > 1)
            {
                throw new ArgumentException();
            }

            foreach (BatsmanInfo item in this)
            {
                array.SetValue(item, arrayIndex);
                arrayIndex++;
            }
        }

        /// <summary>
        /// Provides The Enumerator For The List.
        /// </summary>
        public new IEnumerator<BatsmanInfo> GetEnumerator()
        {
            foreach (BatsmanInfo item in List)
            {
                yield return item;
            }
        }

        /// <summary>
        /// Overload For GetObjectData To Be Used While Serialization.
        /// </summary>
        /// <param name="info">SerializationInfo Object</param>
        /// <param name="context">Context In Which The Object Will Serialize To.</param>
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            foreach (BatsmanInfo batsman in List)
            {
                info.AddValue("Batsman", batsman);
            }
        }

        #endregion
    }

}